#include "board.h"

// constructor
Board::Board()
{
    clear();
}

// helper method to update helper bitboards to current setup
void Board::updateAdditionalBitboards() {
    wPieces = wK | wQ | wR | wB | wN | wP;
    bPieces = bK | bQ | bR | bB | bN | bP;
    occupiedSquares = wPieces | bPieces;
}

void Board::clear() {
    // set all bitboard bits to zero
    wK = wQ = wR = wB = wN = wP = bK = bQ = bR = bB = bN = bP
       = wPieces = bPieces = occupiedSquares = 0x0ULL;
}

// bring board to initial setup position
void Board::init() {
    // clear board
    clear();

    // castle rights set to true for initial position
    wCanCastle00 = wCanCastle000 = bCanCastle00 = bCanCastle000 = true;

    // some test setup
    // working!!!!
//    wQ = Bitutils::Bit(D3);
//    //wK = Bitutils::Bit(A1);
//    wB  = Bitutils::Bit(E2);
//    bP = Bitutils::Bit(H7)+Bitutils::Bit(F7);
//    bN = Bitutils::Bit(B3)+Bitutils::Bit(F8);
//    bB = Bitutils::Bit(G1)+Bitutils::Bit(A7);
//    bQ = Bitutils::Bit(D7);
//    //bK = Bitutils::Bit(A5);
//    bR = Bitutils::Bit(C3)+Bitutils::Bit(F5);

      //wK = Bitutils::Bit(A1);
     // wB  = Bitutils::Bit(E2);  wN = Bitutils::Bit(B5);
//      wP = Bitutils::Bit(H6)+Bitutils::Bit(G2);
//      bP = Bitutils::Bit(A3);//+Bitutils::Bit(F7)+Bitutils::Bit(C7)+Bitutils::Bit(B7);
//      bN = Bitutils::Bit(B3);
     // bB = Bitutils::Bit(A7);
     // bQ = Bitutils::Bit(D7);
      //bK = Bitutils::Bit(A5);
     // bR = Bitutils::Bit(C3);

    // starting pos
    wK = Bitutils::Bit(E1);
    wQ = Bitutils::Bit(D1); wR = Bitutils::Bit(A1)+Bitutils::Bit(H1);
    wB = Bitutils::Bit(C1)+Bitutils::Bit(F1); wN = Bitutils::Bit(B1)+Bitutils::Bit(G1);
    wP = Bitutils::Bit(A2)+Bitutils::Bit(B2)+Bitutils::Bit(C2)+Bitutils::Bit(D2)
         +Bitutils::Bit(E2)+Bitutils::Bit(F2)+Bitutils::Bit(G2)+Bitutils::Bit(H2);
    bK = Bitutils::Bit(E8);
    bQ = Bitutils::Bit(D8); bR = Bitutils::Bit(A8)+Bitutils::Bit(H8);
    bB = Bitutils::Bit(C8)+Bitutils::Bit(F8); bN = Bitutils::Bit(B8)+Bitutils::Bit(G8);
    bP = Bitutils::Bit(A7)+Bitutils::Bit(B7)+Bitutils::Bit(C7)+Bitutils::Bit(D7)
         +Bitutils::Bit(E7)+Bitutils::Bit(F7)+Bitutils::Bit(G7)+Bitutils::Bit(H7);

    updateAdditionalBitboards();

    qDebug() << "Init Board succesful";
}

// set some piece ( type, pos )
void Board::setPiece(unsigned char pieceType, U64 pos) {
    // switch all types
    if (pieceType == EMPTY) return;
    if (pieceType == WHITE_KING)    { wK += pos; return; }
    if (pieceType == WHITE_QUEEN)   { wQ += pos; return; }
    if (pieceType == WHITE_ROOK)    { wR += pos; return; }
    if (pieceType == WHITE_BISHOP)  { wB += pos; return; }
    if (pieceType == WHITE_KNIGHT)  { wN += pos; return; }
    if (pieceType == WHITE_PAWN)    { wP += pos; return; }
    if (pieceType == BLACK_KING)    { bK += pos; return; }
    if (pieceType == BLACK_QUEEN)   { bQ += pos; return; }
    if (pieceType == BLACK_ROOK)    { bR += pos; return; }
    if (pieceType == BLACK_BISHOP)  { bB += pos; return; }
    if (pieceType == BLACK_KNIGHT)  { bN += pos; return; }
    if (pieceType == BLACK_PAWN)    { bP += pos; return; }

    updateAdditionalBitboards();

    qDebug() << "set piece";
}



